package com.cosina.game.tank.view;

import java.util.Iterator;

import android.graphics.Canvas;
import android.graphics.Color;

import com.cosina.game.tank.Constants;
import com.cosina.game.tank.ImageManager;
import com.cosina.game.tank.model.Pill;
import com.cosina.game.tank.model.Tank;
import com.cosina.game.tank.model.War;
import com.cosina.game.tank.model.impl.EnemyTank;

public class WarDrawer {
	
	public static void draw(Canvas canvas, War war){
		init(canvas);
		drawGrid(canvas);
		drawAllTank(canvas, war);
		drawPills(canvas,war);
	}
	
	private static void init(Canvas canvas){
		canvas.drawColor(Color.BLACK);
		canvas.drawRect(Constants.side, PaintSuite.TKV4Bg);
	}
	
	private static void drawGrid(Canvas canvas){
		for(int i=1; i<Constants.XTILE_COUNT*2; i++) {
			canvas.drawLine(Constants.START_X + i* 16, 
							Constants.START_Y+1, 
							Constants.START_X + i* 16, 
							Constants.END_Y -1, PaintSuite.BRV4GRID);
		}
		for(int i=1; i<Constants.YTILE_COUNT*2; i++) {
			canvas.drawLine(Constants.START_X+1, 
							Constants.START_Y + i * 16, 
							Constants.END_X -1, 
							Constants.START_Y + i * 16, PaintSuite.BRV4GRID);
		}
	}
	
	private static void drawTank(Tank tank, Canvas canvas){
		if(tank.isDead())
			return ;
		canvas.drawBitmap(tank.getImage(), tank.getX() + Constants.START_X, tank.getY() + Constants.START_Y, PaintSuite.TKV4Tank);
		if(tank.damage){
			canvas.drawBitmap(ImageManager.damage, tank.getX() + Constants.START_X, tank.getY() + Constants.START_Y, PaintSuite.TKV4Tank);
		}
		if(tank.isSick()){
			canvas.drawBitmap(ImageManager.death[-tank.getHp()], tank.getX() + Constants.START_X, tank.getY() + Constants.START_Y, PaintSuite.TKV4Tank);
		}else{
			int lifeBarLenght = 32* tank.getHp()/tank.getMaxHp();
			canvas.drawLine(tank.getX()  + Constants.START_X, tank.getY() + 32 + Constants.START_Y, tank.getX() + lifeBarLenght  + Constants.START_X, tank.getY()+ 32 + + Constants.START_Y, PaintSuite.TKV4LifeBar);
		}
	}
	private static void drawAllTank(Canvas canvas, War war){
		drawTank(war.justiceTank,canvas);
		
		for(Iterator<EnemyTank> iter = war.enemyTanks.iterator(); iter.hasNext();){
			EnemyTank eachTank = iter.next();
			drawTank(eachTank,canvas);
		}
	}
	private static void drawPills(Canvas canvas, War war){
		for(int i=war.pills.size()-1; i>=0; i--){
			Pill eachPill = war.pills.get(i);
			PaintSuite.TKV4Pill.setColor(eachPill.getColor());
			canvas.drawBitmap(ImageManager.pill, eachPill.getX()-3, eachPill.getY()-3, PaintSuite.simplePaint);
		}
	}
}
